package com.cart.protocol.analyze;

import android.text.TextUtils;

import com.cart.event.ReadSerialEvent;
import com.cart.protocol.ProtocolDefine;

import de.greenrobot.event.EventBus;

/*
    ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ 
       Author   :  lixiaodaoaaa
       Date     :  2020-01-16
       Time     :  17:19
    ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
 */
public abstract class RespAnalyze {
    public String rawDeviceInfo;
    protected int length;

    private int commandResLength = ProtocolDefine.ONE_BYTE_LEN;
    protected int moduleNameLength = ProtocolDefine.ModuleName.length;

    protected String report;
    protected String operatorCmd = "";
    protected String commandWordRes = "";

    public RespAnalyze(String rawDeviceInfo) {
        this.rawDeviceInfo = rawDeviceInfo;
        length = rawDeviceInfo.length();
        int operatorCommandStart = commandResLength + moduleNameLength;
        operatorCmd = rawDeviceInfo.substring(operatorCommandStart, operatorCommandStart + 2)
                                   .toUpperCase();
        commandWordRes = rawDeviceInfo.substring(0, ProtocolDefine.ONE_BYTE_LEN).toUpperCase();
    }

    protected abstract RespAnalyze analyze();

    public void report() {
        analyze();
        if (!TextUtils.isEmpty(report)) {
            EventBus.getDefault().post(new ReadSerialEvent(report));
        }
    }


    public boolean isReadSuccessOrSetSuccess(String commandWordRes) {
        String actionSuccess = ProtocolDefine.CommandWordRes.ACTION_SUCCESS;
        String readSuccess = ProtocolDefine.CommandWordRes.READ_SUCCESS;
        if (commandWordRes.equals(actionSuccess) || commandWordRes.equals(readSuccess)) {
            return true;
        }
        return false;
    }

}
